<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" @submit.native.prevent>
      <el-form-item label="关键字" prop="">
        <el-input v-model="queryParams.search" placeholder="UID、昵称、电话号码、银行、姓名、卡号" clearable style="width: 300px" @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :height="tableHeight" v-loading="loading" :data="bankList" stripe border>
      <el-table-column label="序号" type="index" align="center" width="60" />
      <el-table-column label="UID" prop="userId" align="center" />
      <el-table-column label="用户电话" prop="userInfo.phone" width="120" align="center" />
      <el-table-column label="所属人姓名" prop="cardName" width="100" align="center" />
      <el-table-column label="银行卡号" prop="cardNumber" width="120" align="center" />
      <el-table-column label="银行名称" prop="bankName" width="120" align="center" />
      <el-table-column label="创建时间" prop="createTime" width="160" align="center" />
      <el-table-column label="修改时间" prop="updateTime" min-width="160" align="center" />
      <el-table-column label="银行编码" prop="bankcode" align="center" />
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['member:bankCard:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click.stop="handleDelete(scope.row)" v-hasPermi="['member:bankCard:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="UID" prop="userId">
          <el-input class="form-input" v-model.number="form.userId" :disabled="true" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input class="form-input" v-model="form.nickname" :disabled="true" />
        </el-form-item>
        <el-form-item label="用户电话" prop="phone">
          <el-input class="form-input" v-model.number="form.phone" :disabled="true" />
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName">
          <el-input class="form-input" v-model="form.bankName" :disabled="true" />
        </el-form-item>
<!--        <el-form-item label="银行名称" prop="bankcode">-->
<!--          <el-select class="form-input" v-model="form.bankcode">-->
<!--            <el-option v-for="dict in this.bankinfoList" :key="dict.bankcode" :label="dict.lname" :value="dict.bankcode" @click.native="bankChange(dict)" />-->
<!--          </el-select>-->
<!--        </el-form-item>-->

        <el-form-item label="银行卡所属人姓名" prop="cardName">
          <el-input class="form-input" v-model="form.cardName" placeholder="请输入银行卡所属人姓名" :disabled="true"/>
        </el-form-item>
        <el-form-item label="银行卡号" prop="cardNumber">
          <el-input class="form-input" v-model="form.cardNumber" placeholder="请输入银行卡号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBank,
  addBank,
  updateBank,
  delBank,
  listBankinfo,
} from "@/api/member/appuserbank.js";

export default {
  name: "bankCard", //银行卡
  data() {
    return {
      // 列表数据
      bankList: [],
      bankinfoList: [], //银行网点list
      dialogVisible: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,

      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
        search: undefined,
      },

      // 表单参数
      form: {},

      rules: {
        // bankName: [{ required: true, message: "银行名称不能为空" }],
        cardNumber: [{ required: true, message: "银行卡号不能为空" }],
        cardName: [{ required: true, message: "银行卡所属人姓名不能为空" }],
      },
    };
  },

  computed: {
    tableHeight() {
      return 'calc(100vh - 280px)'
    }
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listBank(this.queryParams).then((response) => {
        if (response.code == 200) {
          this.bankList = response.data;
          this.total = response.total;
        }
        this.loading = false;
      });
    },

    /**获取银行网点列表 */
    getBankInfoList() {
      listBankinfo().then((response) => {
        if (response.code == 200) {
          this.bankinfoList = response.data;
        }
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        bankName: undefined,
        cardNumber: undefined,
        cardName: undefined,
        bankcode: undefined,
        lname: undefined,
        citycode: undefined,
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.title = "编辑银行卡";
      this.form = JSON.parse(JSON.stringify(row));
      this.form.phone = this.form.userInfo.phone;
      this.form.nickname = this.form.userInfo.nickname;
      this.open = true;
      if (!this.bankinfoList.length) {
        this.getBankInfoList();
      }
    },

    /**银行网点选择 */
    bankChange(val) {
      this.form.bankName = val.lname;
    },

    /** 提交按钮 */
    submitForm(from) {
      this.$refs[from].validate((valid) => {
        if (valid) {
          if (this.title == "编辑银行卡") {
            updateBank(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("编辑成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addBank(this.form).then((response) => {
              if (response.code == 200) {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        } else {
          return false;
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除UID为"' + row.userId + '"的数据项？')
        .then(() => {
          delBank({ appUserBankId: row.id }).then((response) => {
            if (response.code == 200) {
              this.$modal.msgSuccess("删除成功");
              this.open = false;
              this.getList();
            }
          });
        })
        .catch(() => { });
    },
  },
};
</script>
<style scoped>
.form-input {
  width: 240px;
}
</style>
